package com.kafka.valid;

import java.util.HashMap;
import java.util.Map;

import net.sf.json.JSONObject;

import com.kafka.bean.Iccard;
import com.kafka.common.Constant;
import com.kafka.common.String.StringValues;
import com.kafka.core.BaseClass;
import com.kafka.enumtype.ResponseType;
import com.kafka.util.CheckUtil;

public class AccountValid extends BaseClass{
	/**
	 * 校验绑定
	 * @param message
	 * @param msgType
	 * @param params
	 * @param result
	 * @param query
	 * @param msgId
	 * @return
	 */
	public static JSONObject validBind(JSONObject params,String msgId) {
		JSONObject result = new JSONObject();
		Map<Object, Object> query = new HashMap<>();
		try {
			//**************************必填字段校验*****************************************
			if(params.getString("uuid").isEmpty()){
				return failMsg(result,StringValues.UUID_CAN_NOT_BE_NULL,ResponseType.DATA_FORMAT_ERROR.toString(),msgId);
			}
			
			if(params.getString("checkpasswd").isEmpty()){
				return failMsg(result,StringValues.CHECKPASSWD_CAN_NOT_BE_NULL,ResponseType.DATA_FORMAT_ERROR.toString(),msgId);
			}
			
			if(params.getString("enpasswd").isEmpty() 
					&& Constant.MUSTCHECK == Integer.valueOf(params.getString("checkpasswd"))){
				return failMsg(result,StringValues.ENPASSWD_CAN_NOT_BE_NULL,ResponseType.DATA_FORMAT_ERROR.toString(),msgId);
			}
			
			if(params.getString("perid").isEmpty()){
				return failMsg(result,StringValues.PERID_CAN_NOT_BE_NULL,ResponseType.DATA_FORMAT_ERROR.toString(),msgId);
			}
			
			if(params.getString("cardno").isEmpty()){
				return failMsg(result,StringValues.CARDNO_CAN_NOT_BE_NULL,ResponseType.DATA_FORMAT_ERROR.toString(),msgId);
			}
			
			if(params.getString("cardtype").isEmpty()){
				return failMsg(result,StringValues.CARDTYPE_CAN_NOT_BE_NULL,ResponseType.DATA_FORMAT_ERROR.toString(),msgId);
			}
			
			//**************************查询字段校验*****************************************
			query.clear();
			query.put("uuid", params.getString("uuid"));
			if (userDao.selectByCondition(query) == null) {
				return failMsg(result,StringValues.UUID_CAN_NOT_BE_NULL,ResponseType.UUID_NOT_FOUND.toString(),msgId);
			}

			query.clear();
			query.put("iccardno", params.getString("cardno"));
			query.put("card_type", params.getString("cardtype"));
			Iccard iccard = iccardDao.selectByCondition(query);
			if ( iccard != null && iccard.getUuid().equals(params.getString("uuid"))) {
				return failMsg(result,StringValues.ICCARD_EXIST,ResponseType.ICCARD_EXIST.toString(),msgId);
			}
			
			//**************************数据格式校验*****************************************
			if(!CheckUtil.isNumber(params.getString("cardtype")) && !params.getString("cardtype").isEmpty()){
				return failMsg(result,StringValues.CARDTYPE_MUST_BE_NUMBER,ResponseType.DATA_FORMAT_ERROR.toString(),msgId);
			}
			
			if(!CheckUtil.isNumber(params.getString("checkpasswd")) && !params.getString("checkpasswd").isEmpty()){
				return failMsg(result,StringValues.CHECKPASSWD_MUST_BE_NUMBER,ResponseType.DATA_FORMAT_ERROR.toString(),msgId);
			}
		} catch (Exception e) {
			e.printStackTrace();
			return failMsg(result,e.toString(),ResponseType.SYSTEM_ERROR.toString(),msgId);
		}
		return null;
	}
	
	/**
	 * 校验解绑
	 * @param message
	 * @param msgType
	 * @param params
	 * @param result
	 * @param query
	 * @param msgId
	 * @return
	 */
	public static JSONObject validUnbind(JSONObject params,String msgId) {
		JSONObject result = new JSONObject();
		Map<Object, Object> query = new HashMap<>();
		try {
			//**************************必填字段校验*****************************************
			if(params.getString("uuid").isEmpty()){
				return failMsg(result,StringValues.UUID_CAN_NOT_BE_NULL,ResponseType.DATA_FORMAT_ERROR.toString(),msgId);
			}

			if(params.getString("cardno").isEmpty()){
				return failMsg(result,StringValues.CARDNO_CAN_NOT_BE_NULL,ResponseType.DATA_FORMAT_ERROR.toString(),msgId);
			}
			
			if(params.getString("cardtype").isEmpty()){
				return failMsg(result,StringValues.CARDTYPE_CAN_NOT_BE_NULL,ResponseType.DATA_FORMAT_ERROR.toString(),msgId);
			}
			
			//**************************查询字段校验*****************************************
			query.clear();
			query.put("uuid", params.getString("uuid"));
			if (userDao.selectByCondition(query) == null) {
				return failMsg(result,StringValues.UUID_NOT_FOUND,ResponseType.UUID_NOT_FOUND.toString(),msgId);
			}

			query.clear();
			query.put("iccardno", params.getString("cardno"));
			Iccard iccard = iccardDao.selectByCondition(query);
			if (iccard == null) {
				return failMsg(result,StringValues.ICCARD_NOT_FOUND,ResponseType.ICCARD_NOT_FOUND.toString(),msgId);
			}
			
			//**************************数据格式校验*****************************************
			if(!CheckUtil.isNumber(params.getString("cardtype")) && !params.getString("cardtype").isEmpty()){
				return failMsg(result,StringValues.CARDTYPE_MUST_BE_NUMBER,ResponseType.DATA_FORMAT_ERROR.toString(),msgId);
			}
		} catch (Exception e) {
			e.printStackTrace();
			return failMsg(result,e.toString(),ResponseType.SYSTEM_ERROR.toString(),msgId);
		}
		return null;
	}
	
	/**
	 * 校验卡挂失
	 * @param message
	 * @param msgType
	 * @param params
	 * @param result
	 * @param query
	 * @param msgId
	 * @return
	 */
	public static JSONObject validLostCard(JSONObject params, String msgId) {
		JSONObject validServiceOperate = validServiceOperate(params,msgId);
		return validServiceOperate;
	}
	
	/**
	 * 激活卡
	 * @param message
	 * @param msgType
	 * @param params
	 * @param result
	 * @param query
	 * @param msgId
	 * @return
	 */
	public static JSONObject validActivateCard(JSONObject params, String msgId) {
		JSONObject validServiceOperate = validServiceOperate(params, msgId);
		return validServiceOperate;
	}
	
	/**
	 * 校验卡黑名单
	 * @param message
	 * @param msgType
	 * @param params
	 * @param result
	 * @param query
	 * @param msgId
	 * @return
	 */
	public static JSONObject validCardBlackList(JSONObject params, String msgId) {
		JSONObject validServiceOperate = validServiceOperate(params, msgId);
		return validServiceOperate;
	}
	
	/**
	 * 业务操作校验 挂失/激活/黑名单
	 * @param message
	 * @param params
	 * @param result
	 * @param query
	 * @param msgId
	 * @return
	 */
	private static JSONObject validServiceOperate(JSONObject params,String msgId) {
		JSONObject result = new JSONObject();
		Map<Object, Object> query = new HashMap<>();
		try {
			//**************************必填字段校验*****************************************
			if(params.getString("uuid").isEmpty()){
				return failMsg(result,StringValues.UUID_CAN_NOT_BE_NULL,ResponseType.DATA_FORMAT_ERROR.toString(),msgId);
			}
			
			if(params.getString("cardno").isEmpty()){
				return failMsg(result,StringValues.CARDNO_CAN_NOT_BE_NULL,ResponseType.DATA_FORMAT_ERROR.toString(),msgId);
			}
			
			if(params.getString("cardtype").isEmpty()){
				return failMsg(result,StringValues.CARDTYPE_CAN_NOT_BE_NULL,ResponseType.DATA_FORMAT_ERROR.toString(),msgId);
			}
			
			//**************************查询字段校验*****************************************

			query.clear();
			query.put("iccardno", params.getString("cardno"));
			Iccard iccard = iccardDao.selectByCondition(query);
			if (iccard == null) {
				return failMsg(result,StringValues.ICCARD_NOT_FOUND,ResponseType.ICCARD_NOT_FOUND.toString(),msgId);
			}
			
			//**************************数据格式校验*****************************************
			if(!CheckUtil.isNumber(params.getString("cardtype")) && !params.getString("cardtype").isEmpty()){
				return failMsg(result,StringValues.CARDTYPE_MUST_BE_NUMBER,ResponseType.DATA_FORMAT_ERROR.toString(),msgId);
			}
			
			return null;
		} catch (Exception e) {
			e.printStackTrace();
			return failMsg(result,e.toString(),ResponseType.SYSTEM_ERROR.toString(),msgId);
		}
	}
	
	/**
	 * 校验修改支付密码
	 * @param message
	 * @param params
	 * @param result
	 * @param query
	 * @param msgId
	 * @return
	 */
	public static JSONObject validUpdatePasswd(JSONObject params, String msgId){
		JSONObject result = new JSONObject();
		Map<Object, Object> query = new HashMap<>();
		try {
			//**************************必填字段校验*****************************************
			if(params.getString("uuid").isEmpty()){
				return failMsg(result,StringValues.UUID_CAN_NOT_BE_NULL,ResponseType.DATA_FORMAT_ERROR.toString(),msgId);
			}
			
			if(params.getString("checkpasswd").isEmpty()){
				return failMsg(result,StringValues.CHECKPASSWD_CAN_NOT_BE_NULL,ResponseType.DATA_FORMAT_ERROR.toString(),msgId);
			}
			
			if(params.getString("enpasswd").isEmpty()){
				return failMsg(result,StringValues.ENPASSWD_CAN_NOT_BE_NULL,ResponseType.DATA_FORMAT_ERROR.toString(),msgId);
			}
			
			if(params.getString("perid").isEmpty()){
				return failMsg(result,StringValues.PERID_CAN_NOT_BE_NULL,ResponseType.DATA_FORMAT_ERROR.toString(),msgId);
			}
			
			if(params.getString("cardno").isEmpty()){
				return failMsg(result,StringValues.CARDNO_CAN_NOT_BE_NULL,ResponseType.DATA_FORMAT_ERROR.toString(),msgId);
			}
			
			if(params.getString("cardtype").isEmpty()){
				return failMsg(result,StringValues.CARDTYPE_CAN_NOT_BE_NULL,ResponseType.DATA_FORMAT_ERROR.toString(),msgId);
			}
			
			//**************************查询字段校验*****************************************
			query.clear();
			query.put("uuid", params.getString("uuid"));
			if (userDao.selectByCondition(query) == null) {
				return failMsg(result,StringValues.UUID_NOT_FOUND,ResponseType.UUID_NOT_FOUND.toString(),msgId);
			}

			query.clear();
			query.put("iccardno", params.getString("cardno"));
			Iccard iccard = iccardDao.selectByCondition(query);
			if ( iccard != null && !iccard.getUuid().equals(params.getString("uuid"))) {
				return failMsg(result,StringValues.ICCARD_EXIST,ResponseType.ICCARD_EXIST.toString(),msgId);
			}
			
			//**************************数据格式校验*****************************************
			if(!CheckUtil.isNumber(params.getString("cardtype")) && !params.getString("cardtype").isEmpty()){
				return failMsg(result,StringValues.CARDTYPE_MUST_BE_NUMBER,ResponseType.DATA_FORMAT_ERROR.toString(),msgId);
			}
			
			if(!CheckUtil.isNumber(params.getString("checkpasswd")) && !params.getString("checkpasswd").isEmpty()){
				return failMsg(result,StringValues.CHECKPASSWD_MUST_BE_NUMBER,ResponseType.DATA_FORMAT_ERROR.toString(),msgId);
			}
		} catch (Exception e) {
			e.printStackTrace();
			return failMsg(result,e.toString(),ResponseType.SYSTEM_ERROR.toString(),msgId);
		}
		return null;
	}
}
